package com.buer.storage.common.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @ProjectName:
 * @Package:        com.buer.storage.common.config.swagger
 * @ClassName:      SwaggerConfig
 * @Description:    java类作用描述
 * @Author:         lds
 * @CreateDate:     2021/1/14 17:23
 * @UpdateUser:     更新者
 * @UpdateDate:     2021/1/14 17:23
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder ticketPar = new ParameterBuilder();
        ParameterBuilder ticketPar1 = new ParameterBuilder();

        ticketPar.name("__request_type").description("请求类型")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数

        ticketPar1.name("__request_token").description("请求标识")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以

        pars.add(ticketPar1.build());    //根据每个方法名也知道当前方法在设置什么参数

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(pars)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.buer.storage.api"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
    	 return new ApiInfoBuilder()
                 .title("springboot利用swagger构建api文档")
                 .description("简单优雅的restful风格接口")
                 .termsOfServiceUrl("http://localhost/swagger-ui.html")
                 .version("1.0")
                 .build();
     }

}
